package com.play.airfight.enemy.enemy_contract;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;

import com.play.airfight.bullets.MyBullet;
import com.play.airfight.GlobalInfo;
import com.play.airfight.managers.BulletsManager;

import java.util.Iterator;


/**
 * 基本的敌人信息
 */
public abstract class BaseEnemy {

    protected int hp = 1; // 生命值 默认 1
    protected int speed = 100; // 移速 默认 1px
    protected boolean isDead = false;  // 是否存活 默认 是
    protected int currentX;   // 当前所在 X
    protected int currentY;   // 当前所在 Y
    protected Bitmap shape;  // 外观

    public abstract EnemyType getEnemyType();

    public void drawFrame(Canvas canvas) {
        Paint p = new Paint();
        canvas.drawBitmap(shape, (float) (currentX - shape.getWidth() / 2), (float) (currentY - shape.getHeight() / 2), p);

    }

    /**
     * 不能为空
     */
    public abstract int setShape();

    private EnemyTypeData enemyTypeData;

    public EnemyTypeData getEnemyTypeData() {
        return enemyTypeData;
    }

    public BaseEnemy(Context context, EnemyTypeData data) {
        enemyTypeData = data;
        data.setEnemyType(getEnemyType());
        hp = data.hp;
        speed = data.speed;
        currentX = data.currentX;
        currentY = data.currentY;
        if (data.customShape != 0) {
            // 自定义的图片
            shape = BitmapFactory.decodeResource(context.getResources(), data.customShape);
        } else {
            // 默认的图片
            shape = BitmapFactory.decodeResource(context.getResources(), setShape());
        }
        // 将高清敌人图片缩放到指定大小
        shape = Bitmap.createScaledBitmap(shape, data.width, data.height, true);
    }

    public abstract boolean isDead();

    public abstract void execLogic();


    //检测碰撞
    protected void detectCollsion(IisCollided iisCollided) {
        // 和子弹碰撞
        Iterator iterator = BulletsManager.myBullets.iterator();
        while (iterator.hasNext()) {
            if (isCollsionWith((MyBullet) iterator.next())) {
                iterator.remove();
                iisCollided.afterCollsion();
                hp -= 1;
            }
        }
        // 和底部碰撞
        // 超出边界 死亡  ,  hp 为0 死亡
        if (currentY >= GlobalInfo.screenH + 20 || (hp <= 0)) {
            isDead = true;
        }
    }

    // 和玩家子弹的碰撞检测
    public boolean isCollsionWith(MyBullet bullet) {
        int x1 = (int) (bullet.currentX - bullet.width / 2);
        int y1 = (int) (bullet.currentY - bullet.height / 2);
        int w1 = bullet.width;
        int h1 = bullet.height;

        int x2 = (int) (currentX - enemyTypeData.width / 2);
        int y2 = (int) (currentY - enemyTypeData.height / 2);
        int w2 = enemyTypeData.width;
        int h2 = enemyTypeData.height;


        //当矩形1位于矩形2的左侧
        if (x1 >= x2 && x1 >= x2 + w2) {
            return false;
            //当矩形1位于矩形2的右侧
        } else if (x1 <= x2 && x1 + w1 <= x2) {
            return false;
            //当矩形1位于矩形2的上方
        } else if (y1 >= y2 && y1 >= y2 + h2) {
            return false;
        } else if (y1 <= y2 && y1 + h1 <= y2) {
            return false;
        }
        //所有不会发生碰撞都不满足时，肯定就是碰撞了
        return true;
    }


    protected interface IisCollided {
        void afterCollsion(); // 碰撞之后
    }
}
